﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HOOMT
{
    public partial class InformationFlowModelForm : OriginalModelForm
    {
        #region 属性
        
        #endregion
        //####################################################################################
        #region 构造函数
        public InformationFlowModelForm(MainForm m)
        {
            //InitializeComponent();
            parentMainForm = m;
            
        }
        #endregion

        #region 窗口属性改变函数集
        //************************************************************************************
        protected override void ModelForm_Click(object sender, EventArgs e)
        {
            this.BringToFront();
            this.Focus();
            parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.stateTransitionsModelToolStrip);
            parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.objectModelToolStrip);
            parentMainForm.splitContainer3.Panel1.Controls.Add(parentMainForm.informationFlowModelToolStrip);
        }
        //************************************************************************************
        protected override void ModelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.informationFlowModelToolStrip);
            e.Cancel = true;
            base.OnClosing(e);
            this.Hide();
        }
        //************************************************************************************
        protected override void ModelForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                this.BringToFront();
                this.Focus();
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.stateTransitionsModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.objectModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Add(parentMainForm.informationFlowModelToolStrip);
            }
            else
            {
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.informationFlowModelToolStrip);
            }
        }
        //************************************************************************************
        //重写WinProc函数拦截消息，处理窗体标题栏点击事件
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0xa1 && (int)m.WParam == 0x2)
            {
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.stateTransitionsModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Remove(parentMainForm.objectModelToolStrip);
                parentMainForm.splitContainer3.Panel1.Controls.Add(parentMainForm.informationFlowModelToolStrip);
            }
            base.WndProc(ref m);
        }
        #endregion

        #region 图形对象鼠标响应函数集
        //************************************************************************************
        protected override void ModelForm_MouseEnter(object sender, EventArgs e)
        {
            //if (this.Focused)
            {
                switch (parentMainForm.currentDiagramFlag)
                {
                    case Diagram.Process:
                        Cursor = System.Windows.Forms.Cursors.Cross;
                        break;
                    case Diagram.InformationSource:
                        Cursor = System.Windows.Forms.Cursors.Cross;
                        break;
                    case Diagram.InformationFlow:
                        Cursor = System.Windows.Forms.Cursors.UpArrow;
                        break;
                    case Diagram.NullObject:
                        Cursor = System.Windows.Forms.Cursors.Default;
                        break;
                }
            }
        }
        //************************************************************************************
        protected override void ModelForm_MouseDown(object sender, MouseEventArgs e)
        {
            #region 左键双击
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                foreach (OriginalDiagram diag in diagramList)
                {
                    if (diag is InformationSourceDiagram)
                    {
                        InformationSourceDiagram obj = (InformationSourceDiagram)diag;
                         Rectangle rect = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height);
                         Point pt = new Point(e.X, e.Y);
                         if (rect.Contains(pt))
                         {
                             if (!obj.HasModelForm)
                             {
                                 InformationFlowModelForm modelForm = new InformationFlowModelForm(parentMainForm);
                                 modelForm.Text = "InformationFlowModelForm/ " + obj.Name;//图形对象名字与其包含的MainForm名字一致

                                 parentMainForm.modelFormList.Add(modelForm);//添加Form对象到MainForm的对象列表modelFormList
                                 modelForm.TopLevel = false;
                                 parentMainForm.splitContainer3.Panel2.Controls.Add(modelForm);
                                 //modelForm.preTreeNode = obj.treeNode;
                                 //增加相应树节点
                                 //TreeNode node = new TreeNode("Main");
                                 //node.ContextMenuStrip = parentMainForm.formMenuStrip;//增加响应菜单（右击）
                                 //node.Tag = modelForm;//节点指向绘制载体
                                 //obj.treeNode.Nodes.Add(node);

                                 obj.NewModelForm = modelForm;
                                 obj.HasModelForm = true;

                                 modelForm.BringToFront();
                                 modelForm.Show();
                                 modelForm.Focus();
                             }
                             else
                             {
                                 ClearSelection();
                                 obj.NewModelForm.BringToFront();
                                 obj.NewModelForm.Show();
                                 obj.NewModelForm.Focus();
                             }
                         }
                    }
                }
            }
            #endregion

            #region 左键单击
            else if (true)//e.Button == MouseButtons.Left)
            {

                ClearSelection();
                Point pt = new Point(e.X, e.Y);
                //添加图形对象
                if ((parentMainForm.currentDiagramFlag == Diagram.Process || parentMainForm.currentDiagramFlag == Diagram.InformationSource))
                {
                    AddDiagram(parentMainForm.currentDiagramFlag, new Point(e.X, e.Y));
                }
                //画线准备
                else if (parentMainForm.currentDiagramFlag == Diagram.InformationFlow)
                {
                    DrawingLinePre(pt);
                }
                //选中操作
                else if (parentMainForm.currentDiagramFlag == Diagram.NullObject)
                {
                    SelectDiagram(pt);
                    SelecteLine(pt);
                }
                //缩放准备
                if (preSelectedDiagram != null)
                {
                    selectedMagnifyingDiagramPoint(pt);
                }
            }
            #endregion

            #region 右击
            if (e.Button == MouseButtons.Right)
            {
                ShowContextMenuStrip(new Point(e.X, e.Y));
                //parentMainForm.currentDiagramFlag = Diagram.NullObject;
                //Cursor = System.Windows.Forms.Cursors.Arrow;
            }
            #endregion

            #region 文字同步
            //使所有对象的NameBox失去焦点
            parentMainForm.informationFlowModelToolStrip.Focus();
            RenameProcess();
            #endregion
        }
        //************************************************************************************
        protected override void ModelForm_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            //移动图形
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovingDiagram(selectedDiagram, pt);
            }
            //缩放图形
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifyingDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawingLine(pt);
            }
        }
        //************************************************************************************
        protected override void ModelForm_MouseUp(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            //移动图形
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovedDiagram(selectedDiagram, pt);
            }
            //缩放图形
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifiedDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawedLine(parentMainForm.currentDiagramFlag, pt);
            }
            base.ModelForm_MouseUp(sender, e);
        }
        #endregion
    }
}
